<?php


class top_user_scores_module extends CI_Module {
	private $limit = 0;
	public function init() {
		$this->load->model("mgamescore");
	}
	
	public function active() {
		
	}
	
	public function deactive() {
		
	}
	
	public function admin($config) {
		$this->view('admin', $config);
	}
	
	/**
	 * 
	 * @param object $config
	 * 
	 */
	public function run($config) {
		$config['view'] = (isset($config['view'] ) && $config['view']) ? $config['view'] : 'home';
		$this->limit = (isset($config['limit'] ) && $config['limit']) ? $config['limit'] : 10;
		switch ($config['view']) {
			case 'all' :
				$config['users'] = $this->scache->script('module.user.scores.get_all','$this->top_user_scores_module->get_all();');
			break;
			case 'cate_parent' :
				$config['users'] = $this->scache->script('module.user.scores.get_all','$this->top_user_scores_module->get_all();');
			break;
			case 'cate_sub' :
				$config['users'] = $this->scache->script('module.user.scores.get_all','$this->top_user_scores_module->get_all();');
			break;
			case 'play' :
				$config['users'] = $this->scache->script('module.user.scores.get_by_game','$this->top_user_scores_module->get_by_game();');
			break;
			default:
				$config['users'] = $this->scache->script('module.user.scores.get_all','$this->top_user_scores_module->get_all();');
			break;
		}
		$this->view('home', $config);
	}

	
	function get_all($limit = 0){
		$this->db->limit($this->limit);
		$this->db->order_by("use_rank asc");
		return $this->muser->gets();
	}
	
	function get_by_game($game_id = 0){
		$this->db->select('game_score.gscore_score as use_scores, u.use_id,u.use_account, u.use_avatar');
		if(is_array($game_id)){
			$this->db->where_in('game_id',$game_id);
		}
		else {
			if($game_id==0)
				$game_id = $this->msystem->get_global('game_id');
			$this->db->where('game_id',$game_id);
		}
		$this->db->join('user as u','u.use_id = game_score.use_id');
		$this->db->where('use_status',1);
		$this->db->limit($this->limit);
		$this->db->order_by('gscore_rank asc');
		return $this->mgamescore->gets();
	}
	
}